package com.gz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gz.common.RedisConst;
import com.gz.entity.Address;
import com.gz.entity.China;
import com.gz.entity.Vo.AddressVo;
import com.gz.service.AddressService;
import com.gz.mapper.AddressMapper;
import com.gz.service.ChinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 龙龙与贝儿
* @description 针对表【t_address】的数据库操作Service实现
* @createDate 2025-05-21 16:31:44
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    ChinaService chinaService;

    @Override
    @Transactional
    public void addAddress(Address address) {
        if (address.getIsDefault()==1){
            addressMapper.update(null,
                    Wrappers.<Address>lambdaUpdate(Address.class)
                            .eq(Address::getUserId,address.getUserId())
                            .set(Address::getIsDefault,0));
        }
        address.setProvince(chinaService.getOne(Wrappers.<China>lambdaQuery(China.class)
                .eq(China::getId,address.getProvince())).getName());
        address.setCity(chinaService.getOne(Wrappers.<China>lambdaQuery(China.class)
                .eq(China::getId,address.getCity())).getName());
        address.setArea(chinaService.getOne(Wrappers.<China>lambdaQuery(China.class)
                .eq(China::getId,address.getArea())).getName());
        addressMapper.insert(address);
        this.updateAddressCache(address.getUserId());
    }

    @Override
    public List<AddressVo> updateAddressCache(Long userId) {
        ValueOperations ops = redisTemplate.opsForValue();
        List<Address> addressData = getAddressData(userId);
        List<AddressVo> addressVos = new ArrayList<>();
        for (Address address : addressData) {
            AddressVo addressVo = new AddressVo();
            addressVo.setId(address.getId());
            addressVo.setIsDefault(address.getIsDefault());
            String addrData=address.getProvince()+"\t"+address.getCity()+"\t"+address.getArea()+"\t"+address.getDetailAddress();
            addressVo.setAddressData(addrData);
            addressVos.add(addressVo);
        }
        ops.set(RedisConst.USER_ADDRESS + ":" + userId, addressVos);
        return addressVos;
    }

    @Override
    public List<Address> getAddressData(Long id) {
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery(Address.class).eq(Address::getUserId,id);
        List<Address> addresses = addressMapper.selectList(wrapper);
        return addresses;
    }
}




